import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.border.TitledBorder;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextField;


public class View extends JFrame {

	public static final Color giantCol = new Color(175, 238, 238);
	public static final Color dwarfsCol = new Color(173, 223, 173);
	private static int move = 1;
	private JPanel contentPane;
	private JTextArea logArea = new JTextArea();
	private JTextField iniPosField;
	private JTextField finPosField;
	private BoardPanel panel_0;
	private int posGig;//da 1 a 8
	private int[] posNani = new int[3];//da 1 a 8 (ognuna)
	private JLabel lblTurno;
	private JButton muoviBtn;

	/**
	 * Create the frame.
	 * @param model 
	 */
	public View() {
		
		setTitle("Caccia al gigante");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(100, 100, 545, 385);//parametri: ?, ?, larghezza, altezza
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(1, 2, 5, 0));
		
		panel_0 = new BoardPanel();
		panel_0.setBorder(new TitledBorder(null, "scacchiera", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		contentPane.add(panel_0);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1);
		panel_1.setLayout(new GridLayout(0, 1, 0, 5));
		
		JPanel panel_1_0 = new JPanel();
		panel_1_0.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "turni", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 51, 51)));
		panel_1.add(panel_1_0);
		panel_1_0.setLayout(new GridLayout(2, 1, 0, 5));
		
		lblTurno = new JLabel();
		lblTurno.setOpaque(true);
		lblTurno.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1_0.add(lblTurno);
		
		JPanel panel_1_0_0 = new JPanel();
		panel_1_0_0.setBorder(new TitledBorder(null, "mossa", TitledBorder.RIGHT, TitledBorder.TOP, null, null));
		panel_1_0.add(panel_1_0_0);
		
		JLabel label = new JLabel("da");
		panel_1_0_0.add(label);
		
		iniPosField = new JTextField();
		iniPosField.setColumns(2);
		panel_1_0_0.add(iniPosField);
		
		JLabel label_5 = new JLabel("a");
		panel_1_0_0.add(label_5);
		
		finPosField = new JTextField();
		finPosField.setColumns(2);
		panel_1_0_0.add(finPosField);
		
		muoviBtn = new JButton("Muovi");
		panel_1_0_0.add(muoviBtn);
		logArea.setEditable(false);
		logArea.setWrapStyleWord(true);
		logArea.setLineWrap(true);
		
		JScrollPane logAreaScrollPane = new JScrollPane(logArea);
		logAreaScrollPane.setViewportBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "log", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(51, 51, 51)));
		logAreaScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		panel_1.add(logAreaScrollPane);
	}
	
	class BoardPanel extends JPanel{
		
		public Color getPosColor (int position) {
			if(position == posGig) return giantCol;
			if(position == posNani[0] || position == posNani[1] || position == posNani[2]) return dwarfsCol;
			return new Color(210, 210, 210);
		}
		
		public void paintComponent (Graphics g) {
			int radius = 35, aHalfRad = Math.round(radius/2);
			
			g.setColor(getPosColor(8));
			g.fillOval(5+3*radius, 15, radius, radius);//posizione 8
			g.setColor(getPosColor(5));
			g.fillOval(5, 15+2*radius, radius, radius);//posizione 5
			g.setColor(getPosColor(6));
			g.fillOval(5+3*radius, 15+2*radius, radius, radius);//posizione 6
			g.setColor(getPosColor(7));
			g.fillOval(5+6*radius, 15+2*radius, radius, radius);//posizione 7
			g.setColor(getPosColor(4));
			g.fillOval(5+3*radius, 15+4*radius, radius, radius);//posizione 4
			g.setColor(getPosColor(2));
			g.fillOval(5, 15+6*radius, radius, radius);//posizione 2
			g.setColor(getPosColor(3));
			g.fillOval(5+6*radius, 15+6*radius, radius, radius);//posizione 3
			g.setColor(getPosColor(1));
			g.fillOval(5+3*radius, 15+8*radius, radius, radius);//posizione 1
			
			g.setColor(Color.DARK_GRAY);
			
		    g.drawString("8", 5+3*radius+aHalfRad, 15+aHalfRad);//numero 8
		    g.drawLine(5+3*radius, 15+aHalfRad, 5+aHalfRad, 15+2*radius);//(8, 5)
		    g.drawLine(5+3*radius+aHalfRad, 15+radius, 5+3*radius+aHalfRad, 15+2*radius);//(8, 6)
		    g.drawLine(5+4*radius, 15+aHalfRad, 5+6*radius+aHalfRad, 15+2*radius);//(8, 7)
		    
		    g.drawString("5", 5+aHalfRad, 15+2*radius+aHalfRad);//numero 5
		    g.drawLine(5+radius, 15+2*radius+aHalfRad, 5+3*radius, 15+2*radius+aHalfRad);//(5, 6)
		    g.drawLine(5+radius, 15+2*radius+aHalfRad, 5+3*radius, 15+4*radius+aHalfRad);//(5, 4)
		    g.drawLine(5+aHalfRad, 15+3*radius, 5+aHalfRad, 15+6*radius);//(5, 2)
		    
		    g.drawString("6", 5+3*radius+aHalfRad, 15+2*radius+aHalfRad);//numero 6
		    g.drawLine(5+3*radius+aHalfRad, 15+3*radius, 5+3*radius+aHalfRad, 15+4*radius);//(6, 4)
		    g.drawLine(5+4*radius, 15+2*radius+aHalfRad, 5+6*radius, 15+2*radius+aHalfRad);//(6, 7)
		    
		    g.drawString("7", 5+6*radius+aHalfRad, 15+2*radius+aHalfRad);//numero 7
		    g.drawLine(5+6*radius, 15+2*radius+aHalfRad, 5+4*radius, 15+4*radius+aHalfRad);//(7, 4)
		    g.drawLine(5+6*radius+aHalfRad, 15+3*radius, 5+6*radius+aHalfRad, 15+6*radius);//(7, 3)
		    
		    g.drawString("4", 5+3*radius+aHalfRad, 15+4*radius+aHalfRad);//numero 4
		    g.drawLine(5+3*radius, 15+4*radius+aHalfRad, 5+radius, 15+6*radius+aHalfRad);//(4, 2)
		    g.drawLine(5+4*radius, 15+4*radius+aHalfRad, 5+6*radius, 15+6*radius+aHalfRad);//(4, 3)
		    g.drawLine(5+3*radius+aHalfRad, 15+5*radius, 5+3*radius+aHalfRad, 15+8*radius);//(4, 1)
		    
		    g.drawString("2", 5+aHalfRad, 15+6*radius+aHalfRad);//numero 2
		    g.drawLine(5+radius, 15+6*radius+aHalfRad, 5+6*radius, 15+6*radius+aHalfRad);//(2, 3)
		    g.drawLine(5+aHalfRad, 15+7*radius, 5+3*radius, 15+8*radius+aHalfRad);//(2, 1)
		    
		    g.drawString("3", 5+6*radius+aHalfRad, 15+6*radius+aHalfRad);//numero 3
		    g.drawLine(5+6*radius+aHalfRad, 15+7*radius, 5+4*radius, 15+8*radius+aHalfRad);//(3, 1)
		    
		    g.drawString("1", 5+3*radius+aHalfRad, 15+8*radius+aHalfRad);//numero 1
		}
	}
	
	public void repaintBoard(int posiz_nani[],int posiz_gigante) {
		
		String lastMove = null;
		if(move>1){
		    if(move%2 == 0){
		    	int i = 0;
		    	while(posNani[i] == posiz_nani[i]+1) i++;
		    	lastMove = "un nano si muove da "+posNani[i]+" a "+(posiz_nani[i]+1);
			}else{
				lastMove = "il gigante si muove da "+posGig+" a "+(posiz_gigante+1);
			}
		    logArea.append(lastMove+"\n");
			logArea.setCaretPosition(logArea.getDocument().getLength());
		}
			
		posGig = posiz_gigante+1;
	    posNani[0] = posiz_nani[0]+1;
	    posNani[1] = posiz_nani[1]+1;
	    posNani[2] = posiz_nani[2]+1;
	    
		panel_0.repaint();//ri-dipinge la "scacchiera"
		
		if(move%2 == 0){
		    lblTurno.setBackground(giantCol);
		    lblTurno.setText("Gigante");
		    iniPosField.setText(Integer.toString(posGig));
		    //iniPosField.setEditable(false);
		}else{
		    lblTurno.setBackground(dwarfsCol);
		    lblTurno.setText("Nani");
		    iniPosField.setText("");
		    //iniPosField.setEditable(true);
		}
		finPosField.setText("");
		move++;
		
	}

	void addMuoviBtnListener(ActionListener muoviBtnListener) {
		muoviBtn.addActionListener(muoviBtnListener);
		
	}
	
	int getIniPos() {
		int pos;
		try{
			pos = Integer.parseInt(iniPosField.getText().trim())-1;
		}catch(NumberFormatException e){
			pos=-1;
		}
		return pos;
	}

	int getFinPos() {
		int pos;
		try{
			pos = Integer.parseInt(finPosField.getText().trim())-1;
		}catch(NumberFormatException e){
			pos=-1;
		}
		return pos;
	}
	
	void log(String message) {
		logArea.append(message+"\n");
		logArea.setCaretPosition(logArea.getDocument().getLength());
	}

	void setEnabledMuovi(boolean enabled){
		muoviBtn.setEnabled(enabled);
		if(!(enabled && move%2 == 1))
			iniPosField.setEditable(enabled);
		finPosField.setEditable(enabled);
	}

}
